﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Calendar = System.Globalization.Calendar;

namespace PersianCalendarControl.Controls
{
    public partial class PersianDatePickerControl : UserControl
    {
        public delegate void CalendarItemClickedEvent(object sender, CalendarEventArgs e);

        public event CalendarItemClickedEvent OnCalendarItemClicked;


        private DateTime _selectedDate = DateTime.Now;
        public DateTime SelectedDate
        {
            get { return _selectedDate; }
            set { _selectedDate = value; }
        }

        private PersianCalendar calendar;
        private PopupMenu _popupMenu;
        private ComposedCalendarViewControl  _calendar;
        public PersianDatePickerControl()
        {
            InitializeComponent();
            //tbSelectedDate.IsReadOnly = true;
            _calendar = new ComposedCalendarViewControl();
            _calendar.OnCalendarItemClicked += new ComposedCalendarViewControl.CalendarItemClickedEvent(Calendar_OnCalendarItemClicked);

            calendar = new PersianCalendar();
            _popupMenu = _calendar;
            _popupMenu.ShowOnRight = true;
            _popupMenu.OffsetY = 25;
            _popupMenu.OffsetX = -185;
            _popupMenu.AddTrigger(TriggerTypes.LeftClick, btnPopup.ButtonControl);

            _year = calendar.GetYear(DateTime.Now);
            _month = calendar.GetMonth(DateTime.Now);
            _day = calendar.GetDayOfMonth(DateTime.Now);



            tbSelectedYear.LostFocus +=new RoutedEventHandler(tbSelectedYear_LostFocus);
            tbSelectedMonth.LostFocus +=new RoutedEventHandler(tbSelectedMonth_LostFocus);
            tbSelectedDay.LostFocus += new RoutedEventHandler(tbSelectedDay_LostFocus);


            tbSelectedYear.TextChanged +=new TextChangedEventHandler(tbSelectedYear_TextChanged);
            tbSelectedMonth.TextChanged +=new TextChangedEventHandler(tbSelectedMonth_TextChanged);
            tbSelectedDay.TextChanged += new TextChangedEventHandler(tbSelectedDay_TextChanged);

            tbSelectedYear.KeyDown += new KeyEventHandler(tbSelectedYear_KeyDown);
            tbSelectedYear.KeyUp += new KeyEventHandler(tbSelectedYear_KeyUp);
            tbSelectedMonth.KeyDown += new KeyEventHandler(tbSelectedMonth_KeyDown);
            tbSelectedDay.KeyDown += new KeyEventHandler(tbSelectedDay_KeyDown);
            tbSelectedDay.KeyUp += new KeyEventHandler(tbSelectedDay_KeyUp);

            
        }

        void tbSelectedDay_KeyUp(object sender, KeyEventArgs e)
        {
            
//            if (tbSelectedDay.Text != string.Empty && (Convert.ToInt32(tbSelectedDay.Text) < 1 || Convert.ToInt32(tbSelectedDay.Text) > 31))
//            {
//                e.Handled = true;
//                return;
//            }
        }

        void tbSelectedYear_KeyUp(object sender, KeyEventArgs e)
        {
            
        }

        void tbSelectedDay_KeyDown(object sender, KeyEventArgs e)
        {
            if((!IsNumericKey(e.Key) ))
            {
                e.Handled = true;
                return;
            }

        }


        private bool IsNumericKey(Key key)
        {
            if(key ==Key.D0 ||key ==Key.D1 ||key ==Key.D2 ||key ==Key.D3 ||key ==Key.D4 ||key ==Key.D5 ||key ==Key.D6 ||key ==Key.D7 ||key ==Key.D8 ||key ==Key.D9 ||
                key ==Key.NumPad0  ||key ==Key.NumPad1  ||key ==Key.NumPad2  ||key ==Key.NumPad3  ||key ==Key.NumPad4  ||key ==Key.NumPad5  ||key ==Key.NumPad6  ||key ==Key.NumPad7  ||key ==Key.NumPad8  ||key ==Key.NumPad9 
                )
            {
                return true;
            }
            return false;
        }


        void tbSelectedMonth_KeyDown(object sender, KeyEventArgs e)
        {
            if ((!IsNumericKey(e.Key)))
            {
                e.Handled = true;
                return;
            }
        }

        void tbSelectedYear_KeyDown(object sender, KeyEventArgs e)
        {
            if ((!IsNumericKey(e.Key)))
            {
                e.Handled = true;
                return;
            }
        }

        private string tbSelectedDayLastText;
        private string tbSelectedMonthLastText;
        private string tbSelectedYearLastText;
        void tbSelectedDay_TextChanged(object sender, TextChangedEventArgs e)
        {

//            if (tbSelectedDay.Text != string.Empty && (Convert.ToInt32(tbSelectedDay.Text) < 1 || Convert.ToInt32(tbSelectedDay.Text) > 31))
//            {
//                tbSelectedDay.Text = tbSelectedDayLastText;
//            }
//            tbSelectedDayLastText = tbSelectedDay.Text;
            UpdateLocals();


            
        }

        void tbSelectedMonth_TextChanged(object sender, TextChangedEventArgs e)
        {
            //            if (tbSelectedMonth.Text != string.Empty && (Convert.ToInt32(tbSelectedMonth.Text) < 1 || Convert.ToInt32(tbSelectedMonth.Text) > 12))
            //            {
            //                tbSelectedMonth.Text = tbSelectedMonthLastText;
            //            }
            //            tbSelectedMonthLastText = tbSelectedMonth.Text;
            UpdateLocals();

        }

        void tbSelectedYear_TextChanged(object sender, TextChangedEventArgs e)
        {
//            if (tbSelectedYear.Text != string.Empty && (Convert.ToInt32(tbSelectedYear.Text) < 1 || Convert.ToInt32(tbSelectedYear.Text) > 2000))
//            {
//                tbSelectedYear.Text = tbSelectedYearLastText;
//            }
//            tbSelectedYearLastText = tbSelectedYear.Text;
            UpdateLocals();
            
        }


        void UpdateLocals()
        {
            if (tbSelectedYear.Text != string.Empty && tbSelectedMonth.Text != string.Empty && tbSelectedDay.Text != string.Empty)
            {
                _year = Convert.ToInt32(tbSelectedYear.Text);
                _month = Convert.ToInt32(tbSelectedMonth.Text);
                _day = Convert.ToInt32(tbSelectedDay.Text);
                this.SelectedDate = calendar.ToDateTime(_year, _month, _day, 0, 0, 0, 0);
                _calendar.CurrentDate = this.SelectedDate;
                 
            }

            
        }
        void tbSelectedDay_LostFocus(object sender, RoutedEventArgs e)
        {
           
        }

        void tbSelectedMonth_LostFocus(object sender, RoutedEventArgs e)
        {
            
        }

        void tbSelectedYear_LostFocus(object sender, RoutedEventArgs e)
        {
           
        }

        void Calendar_OnCalendarItemClicked(object sender, CalendarEventArgs e)
        {
            DateTime SelectedDate = e.SelectedDateTime;
            GetPersianDateString(SelectedDate);
            tbSelectedYear.Text = _year.ToString();
            tbSelectedMonth.Text = _month.ToString();
            tbSelectedDay.Text = _day.ToString();
            _popupMenu.CloseAllOpenMenus();
            if(OnCalendarItemClicked != null) OnCalendarItemClicked(sender,e);
        }


        int _year = 0;
        int _month = 0;
        int _day = 0;

        private string  GetPersianDateString(DateTime dateTime)
        {

            _year = calendar.GetYear(dateTime);
            _month = calendar.GetMonth(dateTime);
            _day = calendar.GetDayOfMonth(dateTime);

            string dateString = _year + "/" + _month + "/" + _day;
            return dateString;
        }
    }
}
